package com.foreknow.service.impl;

import com.foreknow.dao.UserDAO;
import com.foreknow.dao.impl.UserDAOImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.User;
import com.foreknow.service.UserService;

import java.sql.Connection;
import java.util.List;

public class UserServiceImpl implements UserService {



  @Override
  public boolean save(User user) {
    //1.连接数据库
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    UserDAOImpl userDAO = new UserDAOImpl();
    //2.注入连接
    userDAO.setConnection(conn);

    return userDAO.addUser(user);
  }

  @Override
  public boolean del(Integer id) {
    //1.连接数据库
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    UserDAOImpl userDAO = new UserDAOImpl();
    //2.注入连接
    userDAO.setConnection(conn);
    return userDAO.deleteUser(id);
  }

  @Override
  public List<User> getAll() {
    //1.连接数据库
    DBManager dbManager = DBManager.getInstance();
    Connection conn = dbManager.getConnection();
    UserDAOImpl userDAO = new UserDAOImpl();
    //2.注入连接
    userDAO.setConnection(conn);
    return userDAO.getQueryAll();
  }
}
